package App.Repository;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xw on 2018/11/1.
 */
@Component
public class BaseRepository {

    @Autowired
    public EntityManagerFactory entityManagerFactory;

    public static SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
            //sessionFactory = entityManager.unwrap(SessionFactory.class);
        }
        return sessionFactory;
    }

    @Autowired
    EntityManager entityManager;

    public EntityManager getEntityManager() {

        return entityManager;
        // return entityManagerFactory.createEntityManager();
    }


    private List<Map<String, Object>> resultSetToList(ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();
        List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
        while (rs.next()) {
            Map<String, Object> row = new HashMap<String, Object>(columns);
            for (int i = 1; i <= columns; ++i) {
                row.put(md.getColumnName(i), rs.getObject(i));
            }
            rows.add(row);
        }
        return rows;
    }

    //get list
    public <T> List<T> getEntitys(String strSql, Map<String, Object> params, Class entityClass) {
        javax.persistence.Query _query = entityManager.createNativeQuery(strSql, entityClass);
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String _key = entry.getKey();
                Object _value = entry.getValue();
                _query.setParameter(_key, _value);
            }
        }

        return _query.getResultList();
    }


    //新增，更新，删除
    public int updateEntity(String strSql, Map<String, Object> params) {
        javax.persistence.Query _query = entityManager.createNativeQuery(strSql);
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String _key = entry.getKey();
                Object _value = entry.getValue();
                _query.setParameter(_key, _value);
            }
        }
        return _query.executeUpdate();
    }

    //计算count数据
    public int getEntityCount(String strSql, Map<String, Object> params) {
        javax.persistence.Query _query = entityManager.createNativeQuery(strSql);
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String _key = entry.getKey();
                Object _value = entry.getValue();
                _query.setParameter(_key, _value);
            }
        }
        List<BigInteger> _counts = _query.getResultList();
        BigInteger _count = _counts.get(0);
        return _count.intValue();
    }


    public int getMax(String strSql, Map<String, Object> params) {
        javax.persistence.Query _query = entityManager.createNativeQuery(strSql);
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String _key = entry.getKey();
                Object _value = entry.getValue();
                _query.setParameter(_key, _value);
            }
        }
        List<BigInteger> _maxs = _query.getResultList();
        BigInteger _max = _maxs.get(0);
        return _max.intValue();
    }

    public <T> List<T> getEntitValues(String strSql, Map<String, Object> params) {
        javax.persistence.Query _query = entityManager.createNativeQuery(strSql);
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String _key = entry.getKey();
                Object _value = entry.getValue();
                _query.setParameter(_key, _value);
            }
        }
        List<T> _values = _query.getResultList();
        return _values;
    }

    public <T> T getEntitValue(String strSql, Map<String, Object> params) {
        T value = null;
        List<T> _values = getEntitValues(strSql, params);
        if(_values != null && _values.size() > 0){
            value = _values.get(0);
        }
        return value;
    }

    public String checkNull(String _theValue) {

        if (_theValue == null) {
            return "";
        } else {
            return _theValue;
        }
    }

}

